package com.power.sifa.runner.schedule;


import cn.hutool.core.collection.CollectionUtil;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.power.sifa.correction.model.Crc_person;
import com.power.sifa.correction.model.Cre_record_ga;
import com.power.sifa.correction.service.Crc_personService;
import com.power.sifa.correction.service.Cre_record_gaService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName RuleValidateSchedule
 * @Author xiongzw
 * @Date 2023/12/21 10:06
 * @Description 调度任务 执行规则校验
 * @Version: 1.0
 **/

@Component
@RequiredArgsConstructor
public class RuleValidateSchedule {

    private final Crc_personService crc_personService;

    private final Cre_record_gaService cre_record_gaService;


    /**
     * 每10分钟执行一次
     * @throws InterruptedException
     *   @Scheduled(cron = "0 0/10 * * * ?")
     */
//    @Scheduled(cron = "0 0/10 * * * ?")
//    public void taskScheduledOne() throws Exception {
//        // 维度
//        double minlat = 28.607995;
//        double maxlat = 28.761526;
//        Random random = new Random();
//        // 经度
//        double minlng = 115.789490;
//        double maxlng = 116.007053;
//        // 查询人员数据 并且随机抽取5个人员
//        List<Crc_person> personList = crc_personService.findAll();
//        List<Crc_person> validatePersonList = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(personList)) {
//            if (personList.size() > 5) {
//                for (int i = 0; i < 5; i++) {
//                    validatePersonList.add(personList.get((int) Math.random() * personList.size()));
//                }
//            }
//        }
//        // 随机生成当前的人员的数据
//        LocalDateTime nowTime = LocalDateTime.now();
//
//        final String taskId = NanoIdUtils.randomNanoId();
//        if (CollectionUtil.isNotEmpty(validatePersonList)) {
//            validatePersonList.stream().forEach(el -> {
//                // 针对该人员生成前后10分钟的坐标数
//                List<Cre_record_ga> record_gas = new ArrayList<>();
//                for (int index = 0; index < 20; index++) {
//                    Cre_record_ga record_ga = new Cre_record_ga();
//                    record_ga.setId(((int) (Math.random() * 100000)));
//                    record_ga.setPerid(el.getId());
//                    LocalDateTime rectime;
//                    if (index < 10) {
//                        rectime = nowTime.minusMinutes(index);
//                    } else {
//                        rectime = nowTime.plusMinutes(index);
//                    }
//                    // 随机生成坐标数据
//                    double randomLat = minlat + (maxlat - minlat) * random.nextDouble();
//                    double randomLng = minlng + (maxlng - minlng) * random.nextDouble();
//                    record_ga.setLatcamera(randomLat);
//                    record_ga.setLngcamera(randomLng);
//                    record_ga.setRectime(Date.from(rectime.atZone( ZoneId.systemDefault()).toInstant()));
//                    record_ga.setAddr("测试地点" + (int) (Math.random() * 100));
//                    record_ga.setTaskid(taskId);
//                    record_gas.add(record_ga);
//                }
//                cre_record_gaService.saveAll(record_gas);
//            });
//        }
//        // 记录并且执行规则校验
//        cre_record_gaService.validateRecordAsync(taskId);
//
//    }
}
